/**
 * 编辑可视域
 */

import Edit from './Edit'
import IMG_CIRCLE_YELLOW from '../images/circle_yellow.png'
import * as turf from '@turf/turf'
import Transform from '../../transform/Transform'

export default class EditViewShed extends Edit {
  /**
   * 
   * @param {*} overlay 
   */
  constructor(overlay) {
    super()
    this._overlay = overlay
    this._positions = []
    this._message = '点击锚点移动,右击结束编辑'
  }

  _mountEntity() {
    this._positions = this._overlay._positions
    this._delegate = this._overlay
  }

  _mountAnchor() {
    this._positions.forEach((item, index) => {
      this.createAnchor(item, index, index % 2 === 0, index % 2 === 0)
    })
  }

  _onClick(e) {
    e = this.getMouseInfo(e.position)
    if (this._isMoving) {
      this._isMoving = false
      if (this._pickedAnchor && this._pickedAnchor.position) {
        let position = this._clampToGround ? e.surfacePosition : e.position
        if (!position) return
        this._anchorLayer.removeAll()
        this._anchors = []
        this._mountAnchor()
      }
      this._pickedAnchor = undefined
    } else {
      this._isMoving = true
      if (!e.target || !e.target.id) {
        return false
      }
      this._pickedAnchor = e.target.id
      this._pickedAnchor.billboard && this._pickedAnchor.billboard.image.setValue(IMG_CIRCLE_YELLOW)

    }
  }

  /**
   * 鼠标移动
   */
  _onMouseMove(e) {
    e = this.getMouseInfo(e.endPosition)
    this._tooltips.showAt(e.windowPosition, this._message)

    // 选中点位
    if (this._pickedAnchor) {
      let position = this._clampToGround ? e.surfacePosition : e.position
      if (!position) {
        return false
      }

      // 实时更新选中点位的位置
      this._pickedAnchor.position.setValue(position)

      // 不断更新
      this._positions.pop()
      this._positions.push(position)
      // 判断是改变位置还是更改半径
      if (this._pickedAnchor.properties.isMid._value) {
        // 更改可视域的位置
        this._delegate.viewPosition = position

        // 根据角度 距离 点位计算半径点
        let heading = this._delegate.viewHeading
        let distance = this._delegate.viewDistance
        let endPostion = this.computedPoint(position , heading , distance)

        // 更新可视域点位数组
        this._delegate._positions[0] = position
        this._delegate._positions[1] = endPostion

        // 实时更新角度方位
        this._anchors[1].position.setValue(endPostion)
      } else {
        // 改变半径 计算两点之间的角度 正北0
        let startPosition = this._positions[0]
        let angle = this.computedPointAngle(startPosition, position)
        // 计算两点之间的距离 用于角度
        let distance = Cesium.Cartesian3.distance(startPosition, position)

        // 更新可视域位置
        this._delegate.viewDistance = distance
        this._delegate.viewHeading = angle
        // 更新可视域位置数组
        this._delegate._positions[1] = position
      }
    }
  }


  _onRightClick(e) {
    this.unbindEvent()
    this._plotEvent.raiseEvent(this._overlay)
  }

  /**
   * 计算两点之间角度
   * @param {*} startPoint 
   * @param {*} endPoint 
   */

  computedPointAngle(startPoint, endPoint) {
    startPoint = Transform.Cartesian3ToWGS84(this._viewer, startPoint)
    startPoint = [startPoint[0], startPoint[1]]

    endPoint = Transform.Cartesian3ToWGS84(this._viewer, endPoint)
    endPoint = [endPoint[0], endPoint[1]]

    let point1 = turf.point(startPoint)
    var point2 = turf.point(endPoint)

    return turf.bearing(point1, point2)
  }


  /**
   * 取一个点并计算给定的以度、弧度、英里或公里为单位的距离目的地点的位置
   * @param {*} startPoint 
   * @param {*} endPoint 
   */

  computedPoint(position, angle, dis) {
    position = Transform.Cartesian3ToWGS84(this._viewer, position)
    position = [position[0], position[1]]

    let point = turf.point(position)
    let distance = dis
    let bearing = angle
    let options = { units: 'miles' }

    let destination = turf.destination(point, distance / 1609, bearing, options)
    let lng = destination.geometry.coordinates[0]
    let lat = destination.geometry.coordinates[1]
    return new Cesium.Cartesian3.fromDegrees(lng , lat , 0)
  }

  /**
   *
   * @param position
   * @param index
   * @param isMid
   * @param isCenter
   */
  createAnchor(position, index, isMid = false, isCenter = false) {
    let image = isMid
      ? this._options.icon_midAnchor
      : isCenter
        ? this._options.icon_center
        : this._options.icon_anchor
    let anchor = this._anchorLayer.add({
      position: position,
      billboard: {
        image: image,
        width: 12,
        height: 12,
        eyeOffset: new Cesium.Cartesian3(0, 0, -500)
      },
      properties: {
        isMid: isMid,
        index: index,
        message: isMid ? '拖动此点修改半径' : '拖动此点编辑位置'
      }
    })
    this._anchors.push(anchor)
  }
}

